<template>
	<view class="confirmHouseInfo">
		<view class="content" v-for="(item, i) in houseInfo" :key="i">
			<view class="title">
				<text>房源地址: {{ item.houseAddress }}</text>
			</view>
			<view class="content-list">
				<text>房型: {{ item.roomType }}</text>
				<text>开工时间: {{ item.startTime }}</text>
			</view>
			<view class="content-list">
				<text>监理姓名: {{ item.supervisionName || '保密' }}</text>
				<text>工长姓名: {{ item.foremanName || '保密' }}</text>
			</view>
			<view class="content-list">
				<text>瓦工姓名: {{ item.bricklayerName || '保密' }}</text>
				<text>木工姓名: {{ item.carpenterName || '保密' }}</text>
			</view>
			<view class="content-list">
				<text>水电工姓名: {{ item.plumberName || '保密' }}</text>
				<text>油漆工姓名: {{ item.painterName || '保密' }}</text>
			</view>
			<view class="buttom">
				<text>竣工时间: {{ item.endTime || '未竣工' }}</text>
			</view>
			<view class="submitTest">
				<button class="mini-btn" type="default" size="mini" @click="submitAcceptance(item)">确认验收</button>
			</view>
		</view>
		<view class="more" v-if="houseInfo.length === 0">没有信息</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			houseInfo: []
		}
	},
	onLoad() {
		let worker = 'supervisionId'
		const typecode = uni.getStorageSync('userInfo').typecode
		if (typecode === 4) {
			worker = 'supervisionId'
		} else if (typecode === 5) {
			worker = 'foremanId'
		}
		const params = {
			[worker]: uni.getStorageSync('userInfo').id,
			page: 1,
			limit: 99999
		}
		this.init(params)
	},
	methods: {
		init(params) {
			this.$api.getHouseInfos(params)
				.then(res => {
					if (res.success) {
						this.houseInfo = res.rows
					} else {
						uni.showToast({ title: res.msg, icon: 'none' })
					}
				})
				.catch(err => {
					console.log('request fail', err)
				})
		},
		submitAcceptance(item) {
			const typecode = uni.getStorageSync('userInfo').typecode
			if (typecode === 4) {
				// 监理
				if (item.status === 3) {
					uni.showToast({ title: '已经验收过了', icon: 'none' })
					return
				}
				if (item.status === 2) {
					const params = {
						id: item.id,
						status: 3
					}
					this.updateAcceptance(params)
				} else {
					uni.showToast({ title: '工长还没有验收', icon: 'none' })
				}
			} else if (typecode === 5) {
				// 工长
				if (item.status === 2) {
					uni.showToast({ title: '已经验收过了', icon: 'none' })
					return
				}
				const params = {
					id: item.id,
					status: 2
				}
				this.updateAcceptance(params)
			}
		},
		updateAcceptance(params) {
			this.$api.updateHouseInfo(params)
				.then(res => {
					if (res.success) {
						uni.showToast({ title: '验收成功', icon: 'success' })
						setTimeout(() => {
						  uni.navigateBack({
								delta: 1
							})
						}, 500)
					} else {
						uni.showToast({ title: res.msg, icon: 'none' })
					}
				})
				.catch(err => {
					console.log('request fail', err)
				})
		}
	}
}
</script>

<style lang="scss" scoped>
.confirmHouseInfo {
	.content {
		view {
			line-height: 50rpx;
		}
		margin: 20rpx;
		padding: 20rpx;
		box-shadow: 0 0 16rpx #999;
		border-radius: 10rpx;
		.title {
			font-weight: bold;
		}
		.content-list {
			display: flex;
			text {
				width: 50%;
			}
		}
	}
	.more {
		text-align: center;
		padding: 20rpx;
		font-size: 32rpx;
	}
	.submitTest {
		display: flex;
		justify-content: center;
	}
}
</style>
